import {ADD_TODO, SET_VISIBILITY_FILTER, TOGGLE_TODO, VisibilityFilters} from "../Action/ActionTypes";

const initState = {
    visibilityFilter: VisibilityFilters.SHOW_ALL,
    todos:[]
}


//更新todos
/**
 * state = [{text:'',completed:false}]
 *
 * @param state
 * @param action
 * @returns {*}
 */
export const todos = (state=[],action)=>{
    switch (action.type){
        case ADD_TODO:
            return [
                ...state,
                {
                    text:action.text,
                    completed:false
                }
            ]
        case TOGGLE_TODO:
            return state.map((todo,index)=>{
                if(index===action.index){
                    return Object.assign({},todo,{
                        completed:!todo.completed
                    })
                }
                return todo;
            })
        default:
            return state;
    }
}

/**
 * state = {
 *      visibilityFilter:'SHOW_ALL',
 *      todos:[{text:'',completed:false},]
 * }
 * @param state
 * @param action
 * @returns {{} & {visibilityFilter: string, todos: Array} & {visibilityFilter: *}}
 */
const todoApp = (state={},action)=>{
    // switch (action.type){
    //     case SET_VISIBILITY_FILTER:
    //         return Object.assign({},state,{
    //             visibilityFilter:action.filter
    //         })
    //     case ADD_TODO:
    //         return Object.assign({},state,{
    //             todos:todos(state.todos,action)
    //         })
    //     case TOGGLE_TODO:
    //         return Object.assign({},state,{
    //             todos:todos(state.todos,action)
    //         })
    //     default:
    //         return state;
    // }
    /**
     * 合并reducer
     */
    return {
        visibilityFilter: visibilityFilter(state.visibilityFilter,action),
        todos: todos(state.todos,action)
    }
}

/**
 * visibilityFilter Reducer
 */
const {SHOW_ALL} = VisibilityFilters;
export const visibilityFilter = (state = SHOW_ALL,action)=>{
    switch (action.type){
        case SET_VISIBILITY_FILTER:
            return action.filter;
        default:
            return state;
    }
}

// todos 和 filter 业务混合
// const todoApp = (state=initState,action) => {
//     switch (action.type){
//         case SET_VISIBILITY_FILTER:
//             return Object.assign({},state,{
//                 visibilityFilter:action.filter
//             })
//         case ADD_TODO:
//             return Object.assign({},state,{
//                 todos:[
//                     ...state.todos,
//                     {
//                         text:action.text,
//                         completed:false
//                     }
//                 ]
//             })
//         case TOGGLE_TODO:
//             return Object.assign({},state,{
//                 todos:state.todos.map((item,index)=>{
//                     if(index === action.index){
//                         return Object.assign({},item,{
//                             completed:!item.completed
//                         })
//                     }
//                 })
//             })
//
//
//         default:
//             return state
//     }
//
// };
